package main

import "fmt"

func main() {
	//创建结构体
	person := Person{
		name:  "pony",
		age:   18,
		money: Money{999999999, "USD"},
	}
	fmt.Println(person)      //{pony 18}
	fmt.Println(person.name) //pony

	//结构体是值类型（深拷贝）
	person2 := person
	//person的地址是：0xc0000a4018，值是：{pony 18}
	fmt.Printf("person的地址是：%p，值是：%v\n", &person, person)
	//person2的地址是：0xc0000a4048，值是：{pony 18}
	fmt.Printf("person2的地址是：%p，值是：%v\n", &person2, person2)
	//money1的地址是：0xc00006a198，值是：{999999999 USD}
	fmt.Printf("money1的地址是：%p，值是：%v\n", &person.money, person.money)
	//money2的地址是：0xc00006a1f8，值是：{999999999 USD}
	fmt.Printf("money2的地址是：%p，值是：%v\n", &person2.money, person2.money)

	//结构体指针
	p := &person
	fmt.Printf("%p，%T\n", p, p) //0xc00006a180，*main.Person

	//访问结构体字段
	p.money.amount = 0
	fmt.Println(p.money) //{0 USD}

	//用new函数创建指针
	person3 := new(Person)
	fmt.Printf("%p，%T\n", person3, person3) //0xc00006a270，*main.Person
	person3.name = "tony"
	fmt.Println(person3) //&{tony 0 {0 }}

	//匿名结构体
	student := struct {
		name string
		age  int
	}{"Yohann", 18}
	fmt.Println(student) //{Yohann 18}

	//结构体匿名字段（使用类型作为字段名）
	worker := Worker{"jack", 48}
	fmt.Println(worker)        //{jack 48}
	fmt.Println(worker.string) //jack

	//结构体继承 匿名字段可以直接访问（提升字段）
	student1 := Student{person}
	fmt.Println(student1.Person) //{pony 18 {0 USD}}
	fmt.Println(student1.money)  //{0 USD}
	fmt.Println(student1.name)   //pony
}

/*
	公有：字段大写
	私有：字段小写
*/
type Person struct {
	name string
	age  int
	//组合
	money Money
}

type Money struct {
	amount   int
	currency string
}

type Worker struct {
	string
	int
}

type Student struct {
	Person
}
